﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      Search logic
// Created By:     Tingrong Shi (trshi@wicresoft.com)
// Created Date:   2011-2-14
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASBCC.Database.EntityFramework;

namespace ASBCC.Business.CIS
{
    public static class SearchHelper
    {
        /// <summary>
        /// CIO根据主要条件查询Customer
        /// </summary>
        public static string GetCustomerIDsMain(string name, string company, string title, string level, string defination, string area, string country, string editer)
        {
            using (var dc = new CCEntities())
            {
                var customers = dc.SP_CustomerSearchMain(name, company, title, level, defination, area, country, editer).ToList<Customer>();
                string customerIDs = "";
                if (customers == null || customers.Count() == 0)
                    return "-1";
                else
                {
                    foreach (var customer in customers)
                    {
                        customerIDs += customer.CustomerID;
                        customerIDs += ",";
                    }
                    return customerIDs.Substring(0, customerIDs.Length - 1);
                }
            }
        }

        /// <summary>
        /// CID根据主要条件查询Customer
        /// </summary>
        public static string GetCustomerIDsMainCID(string name, string company, string title, string level, string province, string category, string editer)
        {
            using (var dc = new CCEntities())
            {
                var customers = dc.SP_CustomerSearchMainCID(name, company, title, level, province, category, editer).ToList<Customer>();
                string customerIDs = "";
                if (customers == null || customers.Count() == 0)
                    return "-1";
                else
                {
                    foreach (var customer in customers)
                    {
                        customerIDs += customer.CustomerID;
                        customerIDs += ",";
                    }
                    return customerIDs.Substring(0, customerIDs.Length - 1);
                }
            }
        }

        /// <summary>
        /// 根据喜好查询Customer
        /// </summary>
        public static string GetCustomerIDsPrefernce(string list, string value, string level)
        {
            using (var dc = new CCEntities())
            {
                if (list == "0")
                    return "0";
                if (value == null) value = "0";
                if (level == null) level = "0";
                var customers = dc.SP_CustomerSearchPreference(list, value, level).ToList<CustomerPreference>();
                string customerIDs = "";
                if (customers == null || customers.Count() == 0)
                    return "-1";
                else
                {
                    foreach (var customer in customers)
                    {
                        customerIDs += customer.CustomerID;
                        customerIDs += ",";
                    }
                    return customerIDs.Substring(0, customerIDs.Length - 1);
                }
            }
        }

        /// <summary>
        /// 根据学历Customer
        /// </summary>
        public static string GetCustomerIDsEducation(string degree, string school)
        {
            using (var dc = new CCEntities())
            {
                if (degree == "--请选择--" && school == "")
                    return "0";
                var customers = dc.SP_CustomerSearchEducation(degree, school).ToList<Customer>();
                string customerIDs = "";
                if (customers == null || customers.Count() == 0)
                    return "-1";
                else
                {
                    foreach (var customer in customers)
                    {
                        customerIDs += customer.CustomerID;
                        customerIDs += ",";
                    }
                    return customerIDs.Substring(0, customerIDs.Length - 1);
                }
            }
        }

        /// <summary>
        /// 根据工作经历查询Customer
        /// </summary>
        public static string GetCustomerIDsWorkExperience(string name, string title)
        {
            using (var dc = new CCEntities())
            {
                if (name == "" && title == "")
                    return "0";
                var customers = dc.SP_CustomerSearchWorkExp(name, title).ToList<Customer>();
                string customerIDs = "";
                if (customers == null || customers.Count() == 0)
                    return "-1";
                else
                {
                    foreach (var customer in customers)
                    {
                        customerIDs += customer.CustomerID;
                        customerIDs += ",";
                    }
                    return customerIDs.Substring(0, customerIDs.Length - 1);
                }
            }
        }

        /// <summary>
        /// CID根据主要条件查询Company
        /// </summary>
        public static string GetCompanyIDsBMainCID(string companyName, string province, string category, string editer)
        {
            using (var dc = new CCEntities())
            {
                var companies = dc.SP_CompanySearchCID(companyName, province, category, editer).ToList<Company>();
                string companyIDs = "";
                if (companies == null || companies.Count() == 0)
                    return "-1";
                else
                {
                    foreach (var company in companies)
                    {
                        companyIDs += company.CompanyID;
                        companyIDs += ",";
                    }
                    return companyIDs.Substring(0, companyIDs.Length - 1);
                }
            }
        }

        /// <summary>
        /// CIO根据主要条件查询Company
        /// </summary>
        public static string GetCompanyIDsBMainCIO(string companyName, string area, string country, string editer)
        {
            using (var dc = new CCEntities())
            {
                var companies = dc.SP_CompanySearchCIO(companyName, area, country, editer).ToList<Company>();
                string companyIDs = "";
                if (companies == null || companies.Count() == 0)
                    return "-1";
                else
                {
                    foreach (var company in companies)
                    {
                        companyIDs += company.CompanyID;
                        companyIDs += ",";
                    }
                    return companyIDs.Substring(0, companyIDs.Length - 1);
                }
            }
        }

        /// <summary>
        /// 由RoleIDs获取可见的Province
        /// </summary>
        public static IEnumerable GetProvinceByRoleIDs(string[] role, string roleIDs)
        {
            using (var dc = new CCEntities())
            {
                foreach (var role1 in role)
                {
                    if (!String.IsNullOrEmpty(role1))
                    {
                        var role2 = int.Parse(role1);
                        var role3 = dc.Roles.FirstOrDefault(p => p.RoleID == role2);
                        if (role3 != null && role3.BelongsTo == "CID" && role3.IsAdmin == true)
                        {
                            return dc.Provinces.Where(p => p.Disabled == false).ToArray();
                        }
                    }
                }
                return dc.SP_CustomerSearchGetProvince(roleIDs).ToArray();
            }
        }

        /// <summary>
        /// 由RoleIDs获取可见的Category
        /// </summary>
        public static IEnumerable GetCategoryByRoleIDs(string[] role, string roleIDs)
        {
            using (var dc = new CCEntities())
            {
                foreach (var role1 in role)
                {
                    if (!String.IsNullOrEmpty(role1))
                    {
                        var role2 = int.Parse(role1);
                        var role3 = dc.Roles.FirstOrDefault(p => p.RoleID == role2);
                        if (role3 != null && role3.BelongsTo == "CID" && role3.IsAdmin == true)
                        {
                            return dc.Categories.Where(p => p.Disabled == false).ToArray();
                        }
                    }
                }
                return dc.SP_CustomerSearchGetCategory(roleIDs).ToArray();
            }
        }

        /// <summary>
        /// 由RoleIDs获取可见的Country
        /// </summary>
        public static IEnumerable GetCountryByRoleIDs(string[] role, string roleIDs)
        {
            using (var dc = new CCEntities())
            {
                foreach (var role1 in role)
                {
                    if (!String.IsNullOrEmpty(role1))
                    {
                        var role2 = int.Parse(role1);
                        var role3 = dc.Roles.FirstOrDefault(p => p.RoleID == role2);
                        if (role3 != null && role3.BelongsTo == "CIO" && role3.IsAdmin == true)
                        {
                            return dc.Countries.Where(p => p.Disabled == false).ToArray();
                        }
                    }
                }
                return dc.SP_CustomerSearchGetCountry(roleIDs).ToArray();
            }
        }

        /// <summary>
        /// 由RoleIDs获取可见的Area
        /// </summary>
        public static IEnumerable GetAreaByRoleIDs(string[] role, string roleIDs)
        {
            using (var dc = new CCEntities())
            {
                foreach (var role1 in role)
                {
                    if (!String.IsNullOrEmpty(role1))
                    {
                        var role2 = int.Parse(role1);
                        var role3 = dc.Roles.FirstOrDefault(p => p.RoleID == role2);
                        if (role3 != null && role3.BelongsTo == "CIO" && role3.IsAdmin == true)
                        {
                            return dc.Areas.Where(p => p.Disabled == false).ToArray();
                        }
                    }
                }
                return dc.SP_CustomerSearchGetArea(roleIDs).ToArray();
            }
        }

        /// <summary>
        /// 由Area获取可见的Country
        /// </summary>
        public static string GetCountryByareaID(string cil, string areaID)
        {
            using (var dc = new CCEntities())
            {
                var user = dc.Employees.First(p => p.NameEN == cil);
                user.Roles.Load();
                string result="";
                string[] role = new string[user.Roles.Count];
                string roleIDs = "";
                int i = 0;
                foreach (var role1 in user.Roles)
                {
                    role[i] = role1.RoleID.ToString();
                    roleIDs += role1.Name + ",";
                    i++;
                }
                roleIDs = roleIDs.Substring(0, roleIDs.Length - 1);
                var countries = GetCountryByRoleIDs(role, roleIDs);
                if (areaID == "0")
                {
                    foreach(var country in countries)
                    {
                        result += (country as Country).CountryID.ToString() + ";" + (country as Country).Name + "(" + (country as Country).Description + "),";
                    }
                    return result;
                }
                int areaid = int.Parse(areaID);
                var area = dc.Areas.FirstOrDefault(p => p.AreaID == areaid);
                area.Countries.Load();
                foreach (var country in countries)
                {
                    foreach (var country1 in area.Countries)
                    {
                        if ((country as Country).CountryID == country1.CountryID)
                        {
                            result += country1.CountryID.ToString() + ";" + country1.Name + "(" + country1.Description + "),";
                            break;
                        }
                    }
                }
                return result;
            }
        }

        /// <summary>
        /// 获取Definition
        /// </summary>
        public static IEnumerable GetDefinitions()
        {
            using (var dc = new CCEntities())
            {
                return dc.Definitions.Where(p => p.Disabled == false).ToArray();
            }
        }

        /// <summary>
        /// 获取PreferenceList
        /// </summary>
        public static IEnumerable GetPreferenceLists()
        {
            using (var dc = new CCEntities())
            {
                var dcrt = dc.PreferencesLists.Where(p => p.Disabled == false).ToList<PreferencesList>();
                return dc.PreferencesLists.Where(p => p.Disabled == false).ToArray();
            }
        }

        /// <summary>
        /// 由ListID获取PreferenceValue
        /// </summary>
        public static IEnumerable GetPreferenceValues(int listID)
        {
            using (var dc = new CCEntities())
            {
                var list = dc.PreferencesLists.First(p => p.Disabled == false && p.ListID == listID);
                list.PreferencesValues.Load();
                return list.PreferencesValues.ToArray();
            }
        }

        /// <summary>
        /// 由ListID获取PreferenceLevel
        /// </summary>
        public static IEnumerable GetPreferenceLevels(int listID)
        {
            using (var dc = new CCEntities())
            {
                var list = dc.PreferencesLists.First(p => p.Disabled == false && p.ListID == listID);
                list.PreferencesLevels.Load();
                return list.PreferencesLevels.ToArray();
            }
        }
    }
}
